package com.monise.dao.DAO;

import java.sql.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import com.monise.dao.BaseDao.GenericDao;
import com.monise.model.Animal;
import com.monise.model.Cliente;
import com.monise.model.DadosConsulta;

public class DadosConsultaDAO extends GenericDao<DadosConsulta> {

	public List<DadosConsulta> getRetiradas(Date data) {

		EntityManagerFactory factory = Persistence
				.createEntityManagerFactory("pet");
		EntityManager manager = factory.createEntityManager();

		manager.getTransaction().begin();
		Query query = manager
				.createQuery("SELECT a from DadosConsulta a where a.data=:data");
		query.setParameter("data", data);

		@SuppressWarnings("unchecked")
		List<DadosConsulta> consultas = query.getResultList();

		manager.getTransaction().commit();
		manager.close();

		return consultas;
	}

	public List<DadosConsulta> getConsultasFuturas(Animal animal) {

		EntityManagerFactory factory = Persistence
				.createEntityManagerFactory("pet");
		EntityManager manager = factory.createEntityManager();

		manager.getTransaction().begin();
		Query query = manager
				.createQuery("SELECT a from DadosConsulta a where a.animal=:animal and a.pago=:pago");
		query.setParameter("animal", animal);
		query.setParameter("pago", 0);

		@SuppressWarnings("unchecked")
		List<DadosConsulta> consultas = query.getResultList();

		manager.getTransaction().commit();
		manager.close();

		return consultas;
	}

	public List<DadosConsulta> getConsultasFuturas(Cliente cliente) {

		EntityManagerFactory factory = Persistence
				.createEntityManagerFactory("pet");
		EntityManager manager = factory.createEntityManager();

		manager.getTransaction().begin();
		Query query = manager
				.createQuery("SELECT a from DadosConsulta a where a.cliente=:cliente and a.pago=:pago");
		query.setParameter("cliente", cliente);
		query.setParameter("pago", 0);

		@SuppressWarnings("unchecked")
		List<DadosConsulta> consultas = query.getResultList();

		manager.getTransaction().commit();
		manager.close();

		return consultas;
	}

	public List<DadosConsulta> getHistoricoConsultas(Animal animal) {

		EntityManagerFactory factory = Persistence
				.createEntityManagerFactory("pet");
		EntityManager manager = factory.createEntityManager();

		manager.getTransaction().begin();
		Query query = manager
				.createQuery("SELECT a from DadosConsulta a where a.animal=:animal");
		query.setParameter("animal", animal);
		

		@SuppressWarnings("unchecked")
		List<DadosConsulta> consultas = query.getResultList();

		manager.getTransaction().commit();
		manager.close();

		return consultas;
	}
	
	public List<DadosConsulta> getHistoricoConsultas(Cliente cliente) {

		EntityManagerFactory factory = Persistence
				.createEntityManagerFactory("pet");
		EntityManager manager = factory.createEntityManager();

		manager.getTransaction().begin();
		Query query = manager
				.createQuery("SELECT a from DadosConsulta a where a.cliente=:cliente");
		query.setParameter("cliente", cliente);
		

		@SuppressWarnings("unchecked")
		List<DadosConsulta> consultas = query.getResultList();

		manager.getTransaction().commit();
		manager.close();

		return consultas;
	}
	
	public DadosConsulta getDadosConsulta(Long id) {

		EntityManagerFactory factory = Persistence
				.createEntityManagerFactory("pet");
		EntityManager manager = factory.createEntityManager();

		manager.getTransaction().begin();
		Query query = manager
				.createQuery("SELECT a from DadosConsulta a where a.pkDadosConsulta=:id");
		query.setParameter("id", id);

		DadosConsulta consultas = (DadosConsulta) query.getSingleResult();

		manager.getTransaction().commit();
		manager.close();

		return consultas;
	}

}
